const mongoose = require("mongoose");
const Opinion = require("../models/OpinionModel");

exports.addOpinion = async (req, res) => {
  try {
    const newOpinion = await Opinion.create(req.body);
    res.status(201).json({
      status: "success",
      data: newOpinion,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.getOpinion = async (req, res) => {
  try {
    const OpinionData = await Opinion.find();
    res.status(201).json({
      status: "success",
      data: OpinionData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.getOpinionById = async (req, res) => {
  let id = req.params.id;
  try {
    const OpinionData = await Opinion.findOne({ _id: id });
    res.status(201).json({
      status: "success",
      data: OpinionData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.updateOpinion = async (req, res) => {
  console.log("添加评论---", req.body._id);
  try {
    const OpinionData = await Opinion.updateOne(
      { _id: req.body._id },
      req.body
    );
    res.status(201).json({
      status: "success",
      data: OpinionData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};
